package com.zeny.coding.dto;


import com.zeny.coding.util.DtoUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.xml.bind.ValidationException;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDto {
    private String id;

    private String parent;

    private String name;

    private Integer sort;

    private List<CategoryDto> children = new ArrayList<>();

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", parent=").append(parent);
        sb.append(", name=").append(name);
        sb.append(", sort=").append(sort);
        sb.append("]");
        return sb.toString();
    }

    public void isValidForSave() throws ValidationException {
        valid();
        if (!StringUtils.isEmpty(this.id)) {
            this.id = null;
        }
    }

    public void isValidForUpdate() throws ValidationException {
        DtoUtil.valid(this.id, true, 1, 19);
        valid();
    }
    private void valid() throws ValidationException {
        DtoUtil.valid(this.name, true, 1, 50);
        DtoUtil.valid(this.parent, true, 1, 19);
        DtoUtil.require(this.sort);
    }
}